import redis
from config import config


class RedisHandler:
    def __init__(self):
        self.conn = self.__connection()

    def __connection(self):
        redis_config = config["redis_config"]
        conn = redis.StrictRedis(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"],
                                 decode_responses=True, socket_connect_timeout=3)
        return conn

    def get(self, key):
        return self.conn.get(key)


if __name__ == '__main__':
    import time, json
    conn = RedisHandler().conn
    arr = {
        "student_name": '王天天12',
        "times": {'year': '2018', 'month': '08', 'day': '25', 'used_days': 494},
        "step_data": {'all_steps': 7013, 'steps_km': 2.81, 'steps_calorie': 70.13, 'less_carbon_emission': '0.55kg'}
    }
    # conn.hset("22", "22", json.dumps(arr))
    # time.sleep(0.1)
    # result = conn.expire("22", 10)
    result = conn.hget("22", "22")
    # result = conn.exists("1111")
    print(result)
    print("over")
